<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class CustomerIndex extends MY_Controller {
	public function __construct(){
		parent::__construct();
		if(!$this->isLogin()){
			header('location:/Login/login');exit;
		}
		$this->load->model('Customer_model');
		$this->load->model('User_model');
	}
	public function lists(){
		$where=array();
		if(isset($_POST['name'])&&$_POST['name']!=''){
			$name=$_POST['name'];
			$where['A.name']=array('like',trim($name));
		}
		$res=$this->Customer_model->ajax_lists($where);
		foreach($res['list'] as $key=>$value){
			$value['addtime']=date('Y-m-d H:i:s',$value['addtime']);
			if($value['updatetime']!=''){
				$value['updatetime']=date('Y-m-d H:i:s',$value['updatetime']);
			} 
			$res['list'][$key]=$value;
		}
		if(isset($_POST['ajaxs'])){//ajax 获取分页数据
			$this->assign('list',$res['list']);
			$res['list']=$this->fetch('customer/ajaxs_lists.html');
			ajax_return(1,'',$res);
		}else{
			$this->assign('list',$res["list"]);
			$this->assign('pageshow',$res['pageshow']);
			$this->display('customer/lists.html');
		}
	}
	public function add(){
		if($_POST){
			if(!$_POST['name']||$_POST['name']==''){
				ajax_return(false,'名称不能为空');
			}
			if(!$_POST['saleuid']||$_POST['saleuid']==''){
				ajax_return(false,'请对接销售人员');
			}
			$_POST['updatetime']=time();
			$_POST['addtime']=time();
			unset($_POST['ajaxs']);
			$alist=$this->Customer_model->add($_POST);
			if($alist){
				$keywords=$_POST['name']."添加成功";
				useraddrecord('客户添加',$keywords);
				ajax_return(true,'成功');
			}else{//haung 添加
				$keywords=$_POST['name']."添加失败";
				useraddrecord('客户添加',$keywords);
				ajax_return(0,'失败');
			}
		}else{
			$saleuserwhere=array();
			$saleuserwhere["type"]=array("=","1");
			$saleuserlist=$this->Customer_model->user_all($saleuserwhere);
			$this->assign('saleuserlist',$saleuserlist);
			$this->display('customer/add.html');
		}
	}
	public function edit(){
		if($_POST){
			$where=array();
			$where["A.id"]=array("=",$_POST['id']);
			$list=$this->Customer_model->one($where);
			if(!$_POST['name']||$_POST['name']==''){
				ajax_return(false,'名称不能为空');
			}
			if(!$_POST['saleuid']||$_POST['saleuid']==''){
				ajax_return(false,'请对接销售人员');
			}
			if($_POST['saleuid']!=$list['saleuid']){
				$_POST['updatetime']=time();
			}
			unset($_POST['ajaxs']);
			$where=array();
			$where["id"]=array("=",$_POST['id']);
			$alist=$this->Customer_model->edit($where,$_POST);
			if($alist){
				$keywords=$_POST['name']."编辑成功";
				useraddrecord('客户编辑',$keywords);
				ajax_return(true,'成功');
			}else{//haung 添加
				$keywords=$_POST['name']."编辑失败";
				useraddrecord('客户编辑',$keywords);
				ajax_return(0,'失败');
			}
		}else{
			if(isset($_GET['id'])){
				$where=array();
				$where["A.id"]=array("=",$_GET['id']);
				$list=$this->Customer_model->one($where);
				if($list){
					$saleuserwhere=array();
					$saleuserwhere["type"]=array("=","1");
					$saleuserlist=$this->Customer_model->user_all($saleuserwhere);
					$this->assign('list',$list);
					$this->assign('saleuserlist',$saleuserlist);
					$this->display('customer/edit.html');
				}else{
					print("数据错误");
				}
			}else{
				print("数据错误");
			}
		}
	}
	public function del(){
		if($_POST['ajaxs']){
			$where=array();
			$where['A.id']=array('=' , $_POST['id']);
			$list=$this->Customer_model->one($where);
			if(!$list){
				ajax_return(false,'数据错误');
			}
			//判断客服是否在使用huang.
			if($this->Customer_model->getIsProject($_POST['id'])){
				ajax_return(false,'当前客服已经存在项目，不能删除');
			}
			$where=array();
			$where['id']=array('=' , $_POST['id']);
			$dellist=$this->Customer_model->del($where);
			if($dellist){
				$keywords=$list['name']."删除成功";
				useraddrecord('客户删除',$keywords);
				ajax_return(true,'删除成功');	
			}else{
				$keywords=$list['name']."删除失败";
				useraddrecord('客户删除',$keywords);
				ajax_return(false,'删除失败');
			}
		}			
	}
}
